package com.zmm.myyimixian.ui.adapter;

import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zmm.myyimixian.MyApp;
import com.zmm.myyimixian.R;
import com.zmm.myyimixian.bean.SearchResult;
import com.zmm.myyimixian.config.ConfigPath;
import com.zmm.myyimixian.db.ShoppingCart;
import com.zmm.myyimixian.util.UIManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/24 0024.
 * 搜索界面展示搜索数据的adapter
 */

public class SearchAdapter extends BaseAdapter {
    private List<ShoppingCart> mList;
    private List<SearchResult> results;//网络数据商品
    private ModifyCountInterface modifyCountInterface;
    private int count = 0;//默认数量

    //接口回调
    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    public void setShuJuKu(List<ShoppingCart> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void setData(List<SearchResult> results) {
        this.results = results;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return results != null ? results.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return results != null ? results.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_list_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SearchResult result = results.get(position);
        //绑定数据
        Glide.with(parent.getContext()).load(ConfigPath.getLunBoPath(result.getImg())).placeholder(R.mipmap.home_item_default_image).into(viewHolder.searchImg);
        Glide.with(parent.getContext()).load(ConfigPath.getLunBoPath(result.getPromotion_img())).into(viewHolder.searchImgSmail);
        String name = result.getName();
        viewHolder.searchTitle.setText(name);
        viewHolder.searchUnitDesc.setText(result.getUnit_desc());
        viewHolder.searchItemRmb.setText("￥" + result.getUnit_price());
        String unit_old_price = result.getUnit_old_price();
        if ("".equals(unit_old_price)) {
            viewHolder.searchItemOldRmb.setVisibility(View.GONE);
        } else {
            viewHolder.searchItemOldRmb.setVisibility(View.VISIBLE);
            viewHolder.searchItemOldRmb.setText("￥" + result.getUnit_old_price());
            viewHolder.searchItemOldRmb.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        }
        //查询数据跟json中的数据相比较控制控件的状态
        if (MyApp.isLogin() && MyApp.isSJK && mList != null) {
            for (int i = 0; i < mList.size(); i++) {
                ShoppingCart shoppingCart = mList.get(i);
                String title = shoppingCart.getTitle();
                if (name.equals(title)) {
                    viewHolder.searchItemJian.setVisibility(View.VISIBLE);
                    viewHolder.searchItemCountTv.setVisibility(View.VISIBLE);
                    String count1 = shoppingCart.getCount();
                    this.count = Integer.valueOf(count1);
                    viewHolder.searchItemCountTv.setText(count1);
                    break;
                }
                viewHolder.searchItemCountTv.setText("0");
                viewHolder.searchItemJian.setVisibility(View.GONE);
                viewHolder.searchItemCountTv.setVisibility(View.GONE);

            }
        }
        //解决用户在购物车删除所有商品时，本界面数据无法同步问题
        if (mList != null && mList.size() == 0 && MyApp.isSJK) {
            viewHolder.searchItemCountTv.setText("0");
            viewHolder.searchItemJian.setVisibility(View.GONE);
            viewHolder.searchItemCountTv.setVisibility(View.GONE);
        }
        viewHolder.searchItemJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断用户是否登陆
                if (MyApp.isLogin()) {
                    //登陆以后可以将商品添加到购物车
                    modifyCountInterface.doIncrease(position, viewHolder.searchItemCountTv, viewHolder.searchItemJian, viewHolder.searchItemJia, count);
                } else {
                    //用户没登录则跳转到登陆界面
                    UIManager.startLoginActivity(parent.getContext());
                }
            }
        });
        viewHolder.searchItemJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //购物车数量减1
                modifyCountInterface.doDecrease(position, viewHolder.searchItemCountTv, viewHolder.searchItemJian, viewHolder.searchItemJia, count);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.search_img)
        ImageView searchImg;
        @BindView(R.id.search_img_smail)
        ImageView searchImgSmail;
        @BindView(R.id.search_title)
        TextView searchTitle;
        @BindView(R.id.search_unit_desc)
        TextView searchUnitDesc;
        @BindView(R.id.search_item_rmb)
        TextView searchItemRmb;
        @BindView(R.id.search_item_old_rmb)
        TextView searchItemOldRmb;
        @BindView(R.id.search_item_jian)
        ImageButton searchItemJian;
        @BindView(R.id.search_item_count_tv)
        TextView searchItemCountTv;
        @BindView(R.id.search_item_jia)
        ImageButton searchItemJia;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
